package com.xkdx.caipiao.my;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.flowdetail.FlowDetailAction;
import com.xkdx.caipiao.presistence.flowdetail.FlowDetailInfo;
import com.xkdx.caipiao.presistence.flowdetail.FlowDetailModule;
import com.xkdx.caipiao.presistence.flowdetail.FlowDetailPresistence;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;

public class MyFlowActivity extends ThreadActivity {
	FlowDetailAction action;
	FlowDetailModule module;
	FlowDetailPresistence presistence;
	private String pageindex = "1";
	private String pagesize = "1000";
	private String tradestatus = "";
	MyAdapter adapter;
	private ListView lv_flow_detail;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_flow_caipiao);
		lv_flow_detail = (ListView) findViewById(R.id.lv_flow_detail);
		startThread();
		
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		dataLoadDialog.dismiss();
		if (result.containsKey("0")) {
			adapter = new MyAdapter(module.list);
			lv_flow_detail.setAdapter(adapter);

		}

	}
	class Holder {
		TextView pay,flow, phone_numb, time, status;

	}

	class MyAdapter extends BaseAdapter {

		private List<FlowDetailInfo> list2;

		public MyAdapter(ArrayList<FlowDetailInfo> list2) {
			this.list2 = list2;
		}

		public int getCount() {
			return list2.size();
		}

		@Override
		public Object getItem(int position) {
			return list2.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			Holder holder = null;
			if (convertView == null) {
				holder = new Holder();
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.flow_detail_caipiao, null);
				holder.pay = (TextView) convertView.findViewById(R.id.pay);
				holder.flow = (TextView) convertView.findViewById(R.id.tv_flow);
				holder.phone_numb = (TextView) convertView
						.findViewById(R.id.phone_numb);
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.status = (TextView) convertView
						.findViewById(R.id.status);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			float mon1 = Float.valueOf(list2.get(position).getAmount()) / 100;
			DecimalFormat df = new DecimalFormat("#0.00");
			String money = df.format(mon1);
			holder.pay.setText(money + "元");
			holder.flow.setText(list2.get(position).getFlow());
			holder.phone_numb.setText(list2.get(position).getMobileno());
			holder.time.setText(list2.get(position).getCtime());
			String tradestatus = list2.get(position).getTradestatus();
			String ispay = list2.get(position).getIspay();
			String issurepay = list2.get(position).getIssurepay();
			if (ispay.equals("1")||ispay.equals("2")) {
				if (tradestatus.equals("1")) {
					holder.status.setText("充值中");
					holder.status.setTextColor(android.graphics.Color.RED);
				} else if (tradestatus.equals("2")) {
					holder.status.setText("充值中");
					holder.status.setTextColor(android.graphics.Color.RED);
				} else if (tradestatus.equals("3")) {
					holder.status.setText("充值成功");
					holder.status.setTextColor(android.graphics.Color.RED);
				} else if (tradestatus.equals("4")) {
					holder.status.setText("充值失败");
					holder.status.setTextColor(android.graphics.Color.RED);
				} else {
					holder.status.setText("已过期");
					holder.status.setTextColor(0xFFC0C0C0);
				}
			} else {
				if (issurepay.equals("1")) {
					holder.status.setText("充值中");
					holder.status.setTextColor(android.graphics.Color.RED);
				}else {
					holder.status.setText("未支付");
					holder.status.setTextColor(android.graphics.Color.RED);
				}

			}

			return convertView;
		}

	}

	@Override
	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
//		LoginInfo info = getLoginInfo();

		String md5 = usrSP.getId() + usrSP.getLogintoken() + IConstants.key;
		String signcode = Md5Utils.md5(md5);
		action = new FlowDetailAction(usrSP.getId(), usrSP.getLogintoken(),tradestatus,
				pageindex, pagesize, signcode);
		module = new FlowDetailModule();
		presistence = new FlowDetailPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();
	}

	@Override
	public void onBackClick(View view) {
		finish();

	}

}
